// IRIS Citation Importer from Scholar's Portal 
// version 0.1 beta
// 2008-10-28
// Copyright (c) 2008, Brandon Aubie
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.htm
//
// ==UserScript==
// @name            IRIS Citation Importer
// @namespace       http://ireaditsomewhere.googlecode.com
// @description     Quickly import citations from Scholar's Portal
// @include         http://scholarsportal.info/cgi-bin/search.pl*
// @include         http://apps.isiknowledge.com/summary.do?*
// @include         http://www.jneurosci.org/cgi/content/abstract/*
// @include         http://scholar.google.*
// ==/UserScript==


var IRISbase = '';
var image = '';

function gm_xpath(expression,contextNode){
    return document.evaluate(expression,contextNode,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
}

function getParameter(url, parameter){
    if (url != null && url.indexOf('?') != -1){
        var vars = url.split('?')[1].split('&');
        for (var i=0; i < vars.length; i++){
            if (vars[i].split('=')[0] == parameter){
                return vars[i].split('=')[1];
            }
        }
    }
    return null;
}

function render_ScholarsPortal() {

    var allResults = gm_xpath('//a[.="Bibliographic Page"]', document);
    var thisResult, newLink, textSep;
    for (var i = 0; i < allResults.snapshotLength; i++) {
        thisResult = allResults.snapshotItem(i);

        var collection = getParameter(thisResult.getAttribute("href"),'collection');
        var journal = getParameter(thisResult.getAttribute("href"),'journal');
        var issue = getParameter(thisResult.getAttribute("href"),'issue');
        var article = getParameter(thisResult.getAttribute("href"),'article');

        newLink = document.createElement('a');
        newLink.innerHTML = '<img border="0" src="'+importImage+'" />';;
        newLink.setAttribute('href', IRISbase+"/index.php?p=add&import=scholarsportal&collection="+collection+"&journal="+journal+"&issue="+issue+"&article="+article);
        newLink.setAttribute('target', "_new");
        textSep = document.createTextNode(' | ');
        thisResult.parentNode.insertBefore(newLink,thisResult);
        thisResult.parentNode.insertBefore(textSep,thisResult);
    }

}

function render_ISIWebofKnowledge() {
    var allResults = gm_xpath('//td[@class]', document);
    var thisResult, newLink;
    for (var i = 0; i <= allResults.snapshotLength; i++) {
        thisResult = allResults.snapshotItem(i);

        //thisResult.style.backgroundColor = 'yellow';
    }
}

function render_googlescholar() {
    // Journal of Neuroscience
    var allResults = gm_xpath('//a[contains(@href,"jneurosci.org/cgi/content/abstract")]', document);
    var thisResult, newLink;
    for (var i = 0; i < allResults.snapshotLength; i++) {
        thisResult = allResults.snapshotItem(i);
        code = thisResult.getAttribute("href").split("abstract/")[1];
        newLink = document.createElement('a');
        newLink.innerHTML = '<img border="0" src="'+importImage+'" />';;
        newLink.setAttribute('href', IRISbase+"/index.php?p=add&import=jneurosci&code="+code);
        newLink.setAttribute('target', "_new");
        thisResult.parentNode.insertBefore(newLink,thisResult);
    }

    // The Neuroscientist
    var allResults = gm_xpath('//a[contains(@href,"nro.sagepub.com/cgi/content/abstract")]', document);
    var thisResult, newLink;
    for (var i = 0; i < allResults.snapshotLength; i++) {
        thisResult = allResults.snapshotItem(i);
        code = thisResult.getAttribute("href").split("abstract/")[1];
        newLink = document.createElement('a');
        newLink.innerHTML = '<img border="0" src="'+importImage+'" />';;
        newLink.setAttribute('href', IRISbase+"/index.php?p=add&import=spnro&code="+code);
        newLink.setAttribute('target', "_new");
        thisResult.parentNode.insertBefore(newLink,thisResult);
    }
}

function renderPage() {
    checkUserSettings();
    importImage = IRISbase + '/images/import.png';
    var domain = window.location.host;
    switch(domain) {
        case "scholarsportal.info":
            render_ScholarsPortal();
        break;
        case "apps.isiknowledge.com":
            render_ISIWebofKnowledge();
        break;
        case "scholar.google.com":
        case "scholar.google.ca":
            render_googlescholar();
        break;
    }
    GM_registerMenuCommand('Change IRIS Location', changeUserSettings);
}

function changeUserSettings(e) {
    IRISbase = prompt("What is the website URL for your installation of IRIS?\nCurrent Value:", IRISbase);
    GM_setValue('IRISbase', IRISbase);
}

function checkUserSettings() {
    IRISbase = GM_getValue('IRISbase', '');
    if (IRISbase == '-1') {
        IRISbase = prompt("What is the website URL for your installation of IRIS?\nFor example:", "http://www.yourwebsite.com/iris");
        GM_setValue('IRISbase', IRISbase);
    }
}

window.addEventListener("load", renderPage, false);
